package com.ryl.yomall.list.listener;

import com.rabbitmq.client.Channel;
import com.ryl.yomall.list.service.ListService;
import com.ryl.yomall.rabbitmq.constant.MQConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Date 17:57
 * @Author ryl
 */
@Component
public class ESConsumer {
    @Autowired ListService listService;

    @RabbitListener(bindings={@QueueBinding(
            value=@Queue(value = MQConstant.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            exchange=@Exchange(value =MQConstant.EXCHANGE_DIRECT_GOODS ),
            key={MQConstant.ROUTING_GOODS_UPPER}
            )

    })
    public void receiveUpperMessage(Long skuId,Channel channel,Message message){

        try {
            System.out.println ("接收到skuId"+skuId );
            //添加到索引库
            listService.onSale ( skuId );
            channel.basicAck ( message.getMessageProperties ().getDeliveryTag (),false );
        } catch (IOException e) {
            e.printStackTrace ( );
        }
    }
    @RabbitListener(bindings={@QueueBinding(
            value=@Queue(value = MQConstant.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            exchange=@Exchange(value =MQConstant.EXCHANGE_DIRECT_GOODS,type = ExchangeTypes.DIRECT),
            key={MQConstant.ROUTING_GOODS_LOWER}
    )

    })
    public void receiveLowerMessage(Long skuId,Channel channel,Message message){

        try {
            System.out.println ("接收到skuId"+skuId );
            //添加到索引库
            listService.cancelSale ( skuId );
            channel.basicAck ( message.getMessageProperties ().getDeliveryTag (),false );
        } catch (IOException e) {
            e.printStackTrace ( );
        }
    }

}
